import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import dayjs from 'dayjs';

// 定义 state 类型
interface InfoState {
    nickname: string
    gender: string
    country: object
    selectedDate: string;
    language: string
}

const initialState: InfoState = {
    nickname: '',
    gender: '',
    country: {
        isoCode: 'CN',
        name: '中国'
    },
    selectedDate: dayjs().format('YYYY-MM-DD'),
    language: '中文（简体）'

}

let InfoSlice = createSlice({
    name: 'InfoSlice',
    initialState,
    reducers: {
        // 使用 PayloadAction 来标明 action.payload 的类型
        selectGender(state, action: PayloadAction<string>) {
            state.gender = action.payload
        },
        selectCountry(state, action: PayloadAction<object>) {
            let copyState = { ...state }
            copyState.country = action.payload
            state = copyState
            return state
        },
        setDate(state, action: PayloadAction<string>) {
            state.selectedDate = action.payload; // 更新日期
        },
        setNickname(state, action: PayloadAction<string>) {
            state.nickname = action.payload
        },
        setLanguage(state, action: PayloadAction<string>) {
            state.language = action.payload
        }

    }
})

export default InfoSlice

// 导出 action creator
export let { selectGender, selectCountry, setDate, setNickname, setLanguage } = InfoSlice.actions